/**
 * 原型模式
 * 原型模式（Prototype Pattern）是一种创建型模式，克隆已有对象的一种模式。
 * 原型模式的主要目的快速克隆该对象。
 */
public class PrototypePattern {
    public static void main(String[] args) throws CloneNotSupportedException {
//        Prototype prototype = PrototypeFactory.createPrototype();
//        prototype.operation();
////        prototype.cloneNew();
//        ConcretePrototype concretePrototype = new ConcretePrototype("");
//        ArrayList<Object> list = new ArrayList<>();
//        list.add(1);
//        List clone = (List)list.clone();
//        System.out.println(clone.getClass() == list.getClass());
//        System.out.println(clone);
//        clone.add(3);
//        System.out.println(list);

//        ArrayList<Object> originalList = new ArrayList<>();
//        originalList.add("A");
//        originalList.add("B");
//        originalList.add("C");
//
//        // 深拷贝
//        Object deepClonedList = originalList.clone();
//        System.out.println(originalList == deepClonedList);
//        System.out.println("Original List: " + originalList);
//        System.out.println("Deep Cloned List: " + deepClonedList);
//
//        // 浅拷贝
//        List<Object> shallowClonedList = new ArrayList<>(originalList);
//        System.out.println("Original List: " + originalList);
//        System.out.println("Shallow Cloned List: " + shallowClonedList);
//        System.out.println(originalList == shallowClonedList);

        Cal car = new Cal("奔驰","10");
        ConcretePrototype concretePrototype = new ConcretePrototype();
        concretePrototype.setValue("1");
        concretePrototype.setCar(car);
        System.out.println(concretePrototype);

        ConcretePrototype clone = concretePrototype.cloneNew();
        System.out.println(clone);

        car.setAge("19");
        System.out.println(concretePrototype);
        System.out.println(clone);



    }
}

class Cal {
    public String name;
    public String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Cal(String name,String age) {
        this.name = name;
        this.age = age;
    }

    public Cal clone(){
        return new Cal(this.name,this.age);
    }

    @Override
    public String toString() {
        return "car{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

// 原型接口
// 具体原型类
class ConcretePrototype {
    private String value;
    private Cal car;

    public ConcretePrototype() {
    }

    public ConcretePrototype(ConcretePrototype concretePrototype) {
        this.value = concretePrototype.getValue();
        this.car = concretePrototype.getCar().clone();
    }

    public ConcretePrototype cloneNew() throws CloneNotSupportedException {
        return new ConcretePrototype(this);
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Cal getCar() {
        return car;
    }

    public void setCar(Cal car) {
        this.car = car;
    }

    @Override
    public String toString() {
        return "ConcretePrototype{" +
                "value='" + value + '\'' +
                ", list=" + car +
                '}';
    }
}


